/*
 ============================================================================
 Name		: Flashbox4ProMainContainer.cpp
 Author	  : HeQun
 Version	 : 1.0
 Copyright   : 
 Description : CFlashbox4ProMainContainer implementation
 ============================================================================
 */

#include <coemain.h>
#include <EIKENV.H>
#include <eiklabel.h>  
#include <W32STD.H>
#include <stringloader.h>
#include "Flashbox4ProMainContainer.h"
#include "Flashbox4.hrh"
#include "Flashbox4.mbg"
#include "Flashbox4_0x20021B21.rsg"
#include "FbUtils.h"
#include "FbListData.h"




// ============================ MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CFlashbox4AppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFlashbox4ProMainContainer::ConstructL (const TRect& aRect ,TUid aLastViewID, TInt aActiveTabIndex)
{
	// Create a window for this application view
	CreateWindowL ();

	m_activeTabIndex = 0;//-1;


	//Top Tab : contain logo and the topbar
	m_pAppLogo1Img = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Applogo1 );
	
//	m_pTopTabbarImg = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
//			EMbmFlashbox4Tabbar1 );
	
	m_pItembackfocusImg = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Itembackfocus );
	
	m_pItembackImg = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Itemback );
	
	InitializeTabGroupL();

	if(aLastViewID.iUid == ESettingViewId)
	{
		m_activeTabIndex = aActiveTabIndex;
		SetActiveTab();
	}
	////选项菜单列表
	InitialOption();

	//软键区softkeybar
	m_pSoftKeyBar = CFbSoftKeyBar::NewLC (KFbSoftKeyRect, this, this );
	m_pSoftKeyBar->SetLeftKeyL (EMainContainerKeyOption, R_SOFT_KEY_OPTION );//
	m_pSoftKeyBar->SetRightKeyL (EMainContainerKeyExit, R_SOFT_KEY_EXIT );
	CleanupStack::Pop (m_pSoftKeyBar );
	// Set the windows size
	SetRect (aRect );
	// Activate the window, which makes it ready to be drawn
	ActivateL ();
}

CFlashbox4ProMainContainer::~CFlashbox4ProMainContainer ()
{

	delete m_pSoftKeyBar;
	delete m_pOptInfo;
	delete m_pOptionList;
	delete m_pItembackImg;
	delete m_pItembackfocusImg;
	//delete m_pTopTabbarImg;
	delete m_pAppLogo1Img;
	delete m_TabName;
	delete m_Title;
	
	if(m_pTabArrayImg)
	{
		for (TInt i = 0; i < KFbTabitemsCount; i++)
		{
			delete m_pTabArrayImg[i];
		}
	}
	if(m_pTabArrayMaskImg)
	{
		for (TInt i = 0; i < KFbTabitemsCount; i++)
		{
			delete m_pTabArrayMaskImg[i];
		}		
	}

}

// -----------------------------------------------------------------------------
// CFlashbox4AppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CFlashbox4ProMainContainer::Draw (const TRect& /*aRect*/) const
{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc ();
	
	// Gets the control's extent
	TRect drawRect (Rect () );
	
	if(m_pOptionList->IsVisible ())	//Activate Option! Show Alpha Effect!
	{
		gc.SetBrushColor(KFbOptionBackColorAlpha);
		gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
		gc.DrawRect(drawRect);
		return;
	}
	
	gc.SetPenStyle (CGraphicsContext::ESolidPen );
	gc.SetPenColor (KFbTabgroupTopColor );
	gc.SetBrushColor (KFbProMainTabTopColor );
	gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
	gc.DrawRect (drawRect );
	
	
/*	if (m_pTopTabbarImg)
	{
		gc.DrawBitmap(KFbProMainTabTopRect, m_pTopTabbarImg);
	}*/
	
	//-------------Draw Logo & Title---------<BEGIN>
	CFont* font = NULL;//iCoeEnv->NormalFont();
	TFontSpec fontsp = iCoeEnv->NormalFont()->FontSpecInTwips();
	
	iCoeEnv->ScreenDevice()->GetNearestFontInTwips(font, fontsp);
	if (font)
		gc.UseFont(font);
	else
		gc.UseFont(iCoeEnv->NormalFont());
	
	if (m_pAppLogo1Img)
	{
		gc.DrawBitmap(KFbProMainLogo1Rect, m_pAppLogo1Img);
		

		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.SetPenColor(KRgbDarkBlue);
		gc.DrawText(m_Title->Des(), TRect(24, 0, 240, 24), 24,
				CGraphicsContext::ECenter);
	}
	gc.DiscardFont();
	iCoeEnv->ScreenDevice()->ReleaseFont(font);
	//-------------Draw Logo & Title---------<END>
	
	//-------------Draw ItemBar(include ActiveBar) & small Icon---------<BEGIN>
	TRect TabPreViewRect(0,0,240,0);
	TInt j = 0;
	for (TInt i = 0; i < KFbTabitemsCount; i++ )
	{
		if( i == m_activeTabIndex )
		{
			TabPreViewRect.iTl.iY =  m_ActiveRect.iBr.iY;
			TabPreViewRect.iBr.iY =  m_ActiveRect.iBr.iY + KFbProMainTabPreviewRectHeight;
			if (m_pItembackfocusImg)
			{
				gc.DrawBitmap(m_ActiveRect, m_pItembackfocusImg);

				TInt font_oHeight = fontsp.iHeight;
				fontsp.iHeight = 7 * font_oHeight / 10;
				iCoeEnv->ScreenDevice ()->GetNearestFontInTwips (font, fontsp );
				if (font)
					gc.UseFont(font);
				else
					gc.UseFont(iCoeEnv->NormalFont());
				gc.SetPenStyle(CGraphicsContext::ESolidPen);
				gc.SetPenColor(KRgbWhite);
				gc.DrawText(m_TabName->Des(), TPoint(80 + 25,m_ActiveRect.iTl.iY + 28));//TabIconRect.iBr.iX,TabIconRect.iBr.iY
				gc.DiscardFont();
				iCoeEnv->ScreenDevice()->ReleaseFont(font);

			}
			if(m_pTabArrayImg[i] && m_pTabArrayMaskImg[i])
			{
				gc.DrawBitmapMasked (TRect(TPoint(80,m_ActiveRect.iTl.iY + 8),TSize(25,25)), 
						m_pTabArrayImg[i], TRect(m_pTabArrayImg[i]->SizeInPixels()),
						m_pTabArrayMaskImg[i], EFalse );
			}

			gc.SetBrushColor (KFbBackgroundColor);//KRgbWhite
			gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
			gc.DrawRect(TabPreViewRect);
			continue;
		}
		if(m_pItembackImg)
		{
			gc.DrawBitmap(m_UnActiveRect[j], m_pItembackImg);
		}
		if(m_pTabArrayImg[i] && m_pTabArrayMaskImg[i])
		{
			//gc.DrawBitmap(TabIconRect, m_pTabArrayImg[i]);
			gc.DrawBitmapMasked (m_TabIconRect[j], m_pTabArrayImg[i], TRect(m_pTabArrayImg[i]->SizeInPixels()),
					m_pTabArrayMaskImg[i], EFalse );
		}
		j++;
	}
	//-------------Draw ItemBar(include ActiveBar) & small Icon---------<END>
	
	//Logo & Title下的一条白线
	gc.SetPenColor(KRgbWhite);
	gc.DrawLine(TPoint(0,25),TPoint(240,25));
	
}

// -----------------------------------------------------------------------------
// CFlashbox4AppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CFlashbox4ProMainContainer::SizeChanged ()
{
	DrawNow ();
}

/**
 * From CoeControl,CountComponentControls.
 */
TInt CFlashbox4ProMainContainer::CountComponentControls () const
{
	if(m_pOptInfo)
		return 3; // return nbr of controls inside this container
	else
		return 2;
}

/**
 * From CCoeControl,ComponentControl.
 */
CCoeControl* CFlashbox4ProMainContainer::ComponentControl (TInt aIndex ) const
{
	switch (aIndex )
	{
	case 0:
		return m_pSoftKeyBar;
	case 1:
		return m_pOptionList;
	case 2:
		return m_pOptInfo;
/*	case 3:
		return m_pContentWindow;*/
	default:
		return NULL;
	}
}

/**
 * 初始化Tabgroup
 */
void CFlashbox4ProMainContainer::InitializeTabGroupL (/*CFbTabgroupTop *pTabgroup */)
{
	for (TInt i = 0; i < KFbTabitemsCount; i++ )
	{
		TInt resId = EMbmFlashbox4Tabmanhuashu + 2 * i;
		m_pTabArrayImg[i] = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
				resId );
		m_pTabArrayMaskImg[i] = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
				resId + 1 );
		
		if(i < KFbTabitemsCount-1)
		{
			m_UnActiveRect[i].iTl.iX = 0;
			m_UnActiveRect[i].iBr.iX = 240;
			m_UnActiveRect[i].iTl.iY = KFbProMainTabLogo1Height + KFbProMainTabItemFocusHeight +
							 KFbProMainTabPreviewRectHeight + i * KFbProMainTabItemBackHeight;
			m_UnActiveRect[i].iBr.iY = m_UnActiveRect[i].iTl.iY
					+ KFbProMainTabItemBackHeight;
			
			m_TabIconRect[i].iTl.iX = 108;
			m_TabIconRect[i].iBr.iX = 133;
			m_TabIconRect[i].iTl.iY = m_UnActiveRect[i].iTl.iY + 3;
			m_TabIconRect[i].iBr.iY = m_TabIconRect[i].iTl.iY + KFbProMainTabIconHeight;
		}
	}
	m_ActiveRect = TRect(0, 24, 240, 24 + KFbProMainTabItemFocusHeight);	

	m_Title = StringLoader::LoadL (R_PROMAIN_TITLE );
	m_TabName = StringLoader::LoadL (R_TAB_ITEM_TEXT1 );
}

/**
 * From CoeControl
 */
TKeyResponse CFlashbox4ProMainContainer::OfferKeyEventL (const TKeyEvent &aKeyEvent, TEventCode aType )
{
	TKeyResponse keyResponse = EKeyWasNotConsumed;
	//0.当按下功能键时取消菜单显示
	if(EKeyApplication0 == aKeyEvent.iCode && m_pOptionList->IsVisible ())
	{
		m_pOptionList->MakeVisible(EFalse);
		DrawNow();
		return EKeyWasConsumed;
	}
		
	//1.由软键区最先处理按键事件
	if (m_pSoftKeyBar )
	{
		keyResponse = m_pSoftKeyBar->OfferKeyEventL (aKeyEvent, aType );
		if (keyResponse == EKeyWasConsumed )
			return EKeyWasConsumed;
	}
	//2.由选项菜单处理
	if (m_pOptionList && m_pOptionList->IsVisible () )
	{
		return m_pOptionList->OfferKeyEventL (aKeyEvent, aType );
	}
	//3.由关于或帮助页面处理按键
	if (m_pOptInfo && m_pOptInfo->IsVisible () )
	{
		return m_pOptInfo->OfferKeyEventL (aKeyEvent, aType );
	}

	switch (aKeyEvent.iCode)
	{
	case EKeyUpArrow://上拨
		m_activeTabIndex == 0 ? m_activeTabIndex = 6 : m_activeTabIndex--;
		break;
	case EKeyDownArrow://下拨
		m_activeTabIndex >= 6 ? m_activeTabIndex = 0 : m_activeTabIndex++;
		break;
	case EKeyDevice3://确认键
		GetAppUi()->ActiveViewL(EMainViewId, m_activeTabIndex);
		return EKeyWasConsumed;
	default:
		return keyResponse;
	}

	SetActiveTab();

	return keyResponse;
}

/**
 * from MListObserver
 * 选项菜单事件处理
 */
void CFlashbox4ProMainContainer::ListItemEvent (CFbList *pHandle, TInt aItemIndex, TInt /*aItemId*/, TInt aEventType )
{
	if(pHandle == m_pOptionList)
	{
		if(aEventType == EItemSelected)
		{
			CFbListData *pItem = pHandle->GetListItem(aItemIndex);
			TInt seletedItemId = pItem->GetItemId();
			switch (seletedItemId)
			{
			case EOptionId_browser:
				GetAppUi()->ActiveViewL(EMainViewId, m_activeTabIndex);
				break;
			case EOptionId_update:
				break;
			case EOptionId_help:
				if(m_pOptInfo)
					delete m_pOptInfo;
				m_pOptInfo = CFbInformation::NewL(R_INFO_HELP_TITLE,KFbHelpFileName,KFbAboutInfoRect,*this);
				break;
			case EOptionId_about:
				if(m_pOptInfo)
					delete m_pOptInfo;
				m_pOptInfo = CFbInformation::NewL(R_INFO_ABOUT_TITLE,KFbAboutFileName,KFbAboutInfoRect,*this);
				break;
			case EOptionId_setting:
				GetAppUi()->ActiveViewL(ESettingViewId, m_activeTabIndex);
				break;
			case EOptionId_cancel:
				m_pOptionList->MakeVisible(EFalse);
				DrawNow();
				break;
			default:
				break;
			}	
			if(seletedItemId == EOptionId_help || seletedItemId == EOptionId_about)
			{
				m_pOptionList->MakeVisible(EFalse);
				m_pSoftKeyBar->SetLeftKeyL (0, -1);//禁用左键
				m_pSoftKeyBar->SetRightKeyL (EMainContainerKeyReturn, R_SOFT_KEY_RETURN );
				m_pSoftKeyBar->DrawNow();
				m_pOptInfo->DrawNow();
			}
		}
	}

}

/**
 * 派生自SoftKeyBar, 软键事件
 */
void CFlashbox4ProMainContainer::SoftKeyEvent (TInt aKeyId )
{
	switch (aKeyId )
	{
	case EMainContainerKeyOption:
		if (m_pOptionList )
		{
			m_pOptionList->MakeVisible (!m_pOptionList->IsVisible () );			
			DrawNow();
		}
		break;
	case EMainContainerKeyExit:
		if (!m_pOptionList->IsVisible () )
		{
			GetAppUi()->Exit();
			//GetAppUi()->ActiveViewL(EMainViewId, m_activeTabIndex);
		}
		break;
	case EMainContainerKeyReturn:
		m_pSoftKeyBar->SetLeftKeyL (EMainContainerKeyOption, R_SOFT_KEY_OPTION );
		m_pSoftKeyBar->SetRightKeyL (EMainContainerKeyExit, R_SOFT_KEY_EXIT );
		m_pSoftKeyBar->DrawNow();
		m_pOptInfo->MakeVisible(EFalse);
		break;
	default:
		break;
	}	
}

void CFlashbox4ProMainContainer::InitialOption()
{
	//m_pOptionList初始化选项菜单
	TInt OptionLength = 6;
	//const TInt OptProMainArray[] = {0,1,2,3,9,4};
	
	m_pOptionList = CFbList::NewL (KFbOptionRect, this, EStyleOptionMenu);
	m_pOptionList->SetObserver(this);
	
	for (TInt i = 0; i < OptionLength; i++)
	{
		CFbListData *pListItem = new (ELeave) CFbListData(KFbOptionIdArray[EOption_ProMain][i]);
		CleanupStack::PushL(pListItem);
		pListItem->ConstructL(R_OPTION_BROWSER + KFbOptionIdArray[EOption_ProMain][i]);		
		m_pOptionList->AddItemL(pListItem);
		CleanupStack::Pop();//pListItem
	}
	m_pOptionList->SetBackground (KFbOptionFrameColor, KFbOptionBackColor, NULL);
	m_pOptionList->SetTextColor(KRgbWhite,KRgbBlack);
	m_pOptionList->MakeVisible (EFalse );
}
/**
 * 设定激活的栏目
 */
void CFlashbox4ProMainContainer::SetActiveTab()
{
	m_ActiveRect.iTl.iY = KFbProMainTabLogo1Height + m_activeTabIndex * KFbProMainTabItemBackHeight;
	m_ActiveRect.iBr.iY = m_ActiveRect.iTl.iY + KFbProMainTabItemFocusHeight;
	TBool IsActive = EFalse;
	for (TInt i = 0; i < KFbTabitemsCount - 1; i++)
	{
		m_UnActiveRect[i].iTl.iY = KFbProMainTabLogo1Height + i * KFbProMainTabItemBackHeight;
		if(m_activeTabIndex == i)
		{
			IsActive = ETrue;
		}
		if(IsActive)
		{
			m_UnActiveRect[i].iTl.iY += KFbProMainTabItemFocusHeight + KFbProMainTabPreviewRectHeight;
		}		
		m_UnActiveRect[i].iBr.iY = m_UnActiveRect[i].iTl.iY	+ KFbProMainTabItemBackHeight;
		m_TabIconRect[i].iTl.iY = m_UnActiveRect[i].iTl.iY + 3;
		m_TabIconRect[i].iBr.iY = m_TabIconRect[i].iTl.iY + KFbProMainTabIconHeight;
	}
	if(m_TabName)
	{
		delete m_TabName;
	}
	m_TabName = StringLoader::LoadL (R_TAB_ITEM_TEXT1 + m_activeTabIndex);
	DrawNow();
}

// End of File
